package com.kly.cashmall.module.mine.presenter;

import com.kly.cashmall.bean.MessageBean;
import com.kly.cashmall.framework.mvp.BaseViewPresenter;
import com.kly.cashmall.services.Network;
import com.kly.cashmall.services.Params;
import com.kly.cashmall.utils.function.ToastUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


public class MessagePresenter extends BaseViewPresenter<MessageViewer> {

    public MessagePresenter(MessageViewer viewer) {
        super(viewer);
    }

    @Override
    public void willDestroy() {

    }
    public void getMessageList(int pageNo){
        Params params = new Params();
        params.put("pageNo", pageNo);
        rxManager.add(Network.getApi().getMessageList(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<MessageBean>() {
            @Override
            public void accept(MessageBean bean) throws Exception {
                if (getViewer() != null) {
                    if (pageNo == 1){
                        getViewer().getMessageListRefreshSuccess(bean);
                    }else {
                        getViewer().getMessageListLoadMoreSuccess(bean);
                    }
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                ToastUtil.showAppToast(throwable.getMessage());
            }
        }));
    }
}